package util;

import ids.ProbeSensor;

import java.io.File;
import java.util.Vector;

import ns2.Field;
import ns2.Topology;

public class ListAllAttacks {

	public Topology topology;

	public ListAllAttacks(FileReader fileReader) {
		this.topology = fileReader.getTopology();
	}

	public String listProbeAttacks() {
		StringBuffer result = new StringBuffer();
		Connections connections = new Connections();
		int totalAttacks = 0;
		for (int n = 0; n < this.topology.nodes.size(); n++) {
			connections.addAll(this.topology.nodes.nodeAt(n).connections);
		}

		Vector<Connections> groupBySources = connections.sort(Field.Src_addr);

		for (int i = 0; i < groupBySources.size(); i++) {
			if (groupBySources.elementAt(i).size() >= ProbeSensor.lower_threshold) {
				totalAttacks += groupBySources.elementAt(i).size();
				for (int j = 0; j < groupBySources.elementAt(i).size(); j++) {
					result.append(groupBySources.elementAt(i).elementAt(j)
							.getSrc()
							+ " attacks "
							+ groupBySources.elementAt(i).elementAt(j).getDst()
							+ "\n");
				}
			}
		}
		result.append("Total Attacks: " + totalAttacks + "\n");
		return result.toString();
	}

	public static void main(String[] args) {
		FileReader fileReader = null;
		try {
			fileReader = new FileReader(new File(args[0]), new File(args[1]),
					new File(args[2]));
			fileReader.readTraceFile();
			fileReader.readTopologyFile();
			fileReader.process();
		} catch (SSIDException e) {
			System.err.println(e.getMessage());
			System.err.print(e.getExSearchUsage());
			System.exit(1);
		}
		ListAllAttacks laa = new ListAllAttacks(fileReader);
		System.out.print(laa.listProbeAttacks());
	}
}
